#!/bin/sh
# Copyright © 2005-2007  Roger Leigh <rleigh@codelibre.net>
#
# schroot is free software: you can redistribute it and/or modify it
# under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# schroot is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
# General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see
# <http://www.gnu.org/licenses/>.
#
#####################################################################

set -e

. "$SETUP_DATA_DIR/common-data"
. "$SETUP_DATA_DIR/common-functions"
. "$SETUP_DATA_DIR/common-config"

# Check file type
check_filetype()
{
    if echo "$CHROOT_FILE" | grep -q '\.tar$'; then
        filetype="tar"
    elif echo "$CHROOT_FILE" | egrep -q '(\.tar\.gz|\.tgz)$'; then
        filetype="tgz"
    elif echo "$CHROOT_FILE" | egrep -q '(\.tar\.bz2|\.tbz)$'; then
        filetype="tbz"
    elif echo "$CHROOT_FILE" | egrep -q '(\.tar\.xz|\.txz)$'; then
        filetype="txz"
    elif echo "$CHROOT_FILE" | egrep -q '(\.tar\.lzo|\.tzo)$'; then
        filetype="tzo"
    elif echo "$CHROOT_FILE" | egrep -q '(\.tar\.lz4|\.tlz4)$'; then
        filetype="tlz4"
    elif echo "$CHROOT_FILE" | egrep -q '\.tar\.zstd$'; then
        filetype="tzstd"
    else
        fatal "Unsupported filetype for $CHROOT_FILE"
    fi
}

# Unpack archive
unpack_file()
{
    if [ ! -f "$CHROOT_FILE" ]; then
        fatal "File '$CHROOT_FILE' does not exist"
    fi

    if [ "$filetype" = "tar" ]; then
        tar $TAR_VERBOSE -xf "$CHROOT_FILE"
    elif [ "$filetype" = "tgz" ]; then
        tar $TAR_VERBOSE -xzf "$CHROOT_FILE"
    elif [ "$filetype" = "tbz" ]; then
        tar $TAR_VERBOSE -xjf "$CHROOT_FILE"
    elif [ "$filetype" = "txz" ]; then
        tar $TAR_VERBOSE -xJf "$CHROOT_FILE"
    elif [ "$filetype" = "tzo" ]; then
        tar $TAR_VERBOSE --lzop -xf "$CHROOT_FILE"
    elif [ "$filetype" = "tlz4" ]; then
        lz4 -cd "$CHROOT_FILE" | tar $TAR_VERBOSE -x
    elif [ "$filetype" = "tzstd" ]; then
        zstd -cd "$CHROOT_FILE" | tar "$TAR_VERBOSE" -x
    else
        fatal "Unsupported filetype for $CHROOT_FILE"
    fi
}

# Repack archive
repack_file()
{
    NEWFILE=$(mktemp "${CHROOT_FILE}.XXXXXX")

    trap "if [ -f \"$NEWFILE\" ]; then rm -f \"$NEWFILE\"; fi" 0

    if [ "$filetype" = "tar" ]; then
        tar $TAR_VERBOSE -cf "$NEWFILE" .
    elif [ "$filetype" = "tgz" ]; then
        tar $TAR_VERBOSE -czf "$NEWFILE" .
    elif [ "$filetype" = "tbz" ]; then
        tar $TAR_VERBOSE -cjf "$NEWFILE" .
    elif [ "$filetype" = "txz" ]; then
        tar $TAR_VERBOSE -cJf "$NEWFILE" .
    elif [ "$filetype" = "tzo" ]; then
        tar $TAR_VERBOSE --lzop -cf "$NEWFILE" .
    elif [ "$filetype" = "tlz4" ]; then
        tar $TAR_VERBOSE -c . | lz4 > "$NEWFILE"
    elif [ "$filetype" = "tzstd" ]; then
        tar "$TAR_VERBOSE" -c . | zstd > "$NEWFILE"
    else
        fatal "Unsupported filetype for $CHROOT_FILE"
    fi

    if [ -f "$CHROOT_FILE" ]; then
        info "Setting ownership and permissions from old archive"
        chown --reference="$CHROOT_FILE" "$NEWFILE"
        chmod --reference="$CHROOT_FILE" "$NEWFILE"
    else
        warn "Old archive no longer exists"
        warn "Setting ownership and permissions to root:root 0600"
        chown root:root "$NEWFILE"
        chmod 0600 "$NEWFILE"
    fi
    mv "$NEWFILE" "$CHROOT_FILE"

    trap "" 0
}

if [ "$VERBOSE" = "verbose" ]; then
    TAR_VERBOSE="-v"
fi

if [ "$CHROOT_TYPE" = "file" ]; then

    check_filetype

    UNPACK_LOCATION="${CHROOT_FILE_UNPACK_DIR}/${SESSION_ID}"

    if [ $STAGE = "setup-start" ]; then

        info "File unpack directory: $UNPACK_LOCATION"

        if [ ! -d "$UNPACK_LOCATION" ]; then
            mkdir -p "$UNPACK_LOCATION"
            info "Created file unpack directory: $UNPACK_LOCATION"
        fi
        cd "$UNPACK_LOCATION"
        info "Changed CWD to $UNPACK_LOCATION"

        unpack_file

    elif [ "$STAGE" = "setup-stop" ]; then

        if [ "$STATUS" = "ok" ] && [ "$CHROOT_FILE_REPACK" = "true" ]; then
            if [ -d "$UNPACK_LOCATION" ]; then
                info "Repacking chroot archive file $CHROOT_FILE from $UNPACK_LOCATION"
                cd "$UNPACK_LOCATION" && repack_file
            else
                warn "Not repacking chroot archive file: $UNPACK_LOCATION does not exist (it may have been removed previously)"
            fi
        fi

        if [ "$CHROOT_SESSION_PURGE" = "true" ]; then
            info "Purging $UNPACK_LOCATION"
            if [ -d "$UNPACK_LOCATION" ]; then
                rm -rf "$UNPACK_LOCATION"
            fi
        fi

    fi

fi

